#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;


class LuckySum {
	int static const maxn = 15;
	long long static const inf = 1ll << 50;
	long long const opt[6] = {1, 2, 4, 5, 8, 9};
	long long pow_ten[maxn];
	long long f[maxn][10][2];
	public:
		bool legal(char ch)
		{
			return (ch == '4' || ch == '1' || ch == '2' || ch == '5' ||
						ch == '8' || ch == '9' || ch == '?');
		}

		void init_pow_ten()
		{
			pow_ten[0] = 1;
			for (int i = 1; i < maxn; i++) pow_ten[i] = pow_ten[i-1] * 10;
		}

		long long construct(string note) {
			int len = note.length();
			for (int i = 0; i < len; i++)
				if (!legal(note[i])) return -1;

			init_pow_ten();
			for (int i = 0; i < len; i++)
				for (int j = 0; j < 10; j++) f[i][j][0] = f[i][j][1] = inf;

			long long ans = inf;
			if (note[len-1] == '?') {
				f[len-1][8][0] = 8;
				f[len-1][1][0] = 1;
				f[len-1][4][0] = 4;
				f[len-1][7][1] = 7;
				f[len-1][4][1] = 4;
				if (len == 1) ans = 8;
			} else {
				long long now = note[len-1] - '0';
				if (now == 2 || now == 5 || now == 9) return -1;
				if (now != 7)
					f[len-1][now][0] = now;
				if (now == 7 || now == 4)
					f[len-1][now][1] = now;
				if (len == 1 && now == 8) ans = 8;
			}
			for (int i = len-2; i >= 0; i--) {
				if (note[i] == '?') {
					long long min0 = inf, min1 = inf;
					for (int j = 0; j < 6; j += 2)
						min0 = std::min(min0, f[i+1][opt[j]][0]);
					for (int j = 1; j < 6; j += 2)
						min1 = std::min(min1, f[i+1][opt[j]][0]);
					if (min0 < inf)
						for (int j = 0; j < 6; j += 2) {
							f[i][opt[j]][0] = min0 + opt[j] * pow_ten[len - i - 1];
							if (!i)
								ans = std::min(ans, f[i][opt[j]][0]);
						}
					if (min1 < inf)
						for (int j = 1; j < 6; j += 2) {
							f[i][opt[j]][0] = min1 + opt[j] * pow_ten[len - i - 1];
							if (!i)
								ans = std::min(ans, f[i][opt[j]][0]);
						}
					f[i][4][1] = std::min(f[i+1][4][1], f[i+1][7][1]) + 4 * pow_ten[len - i - 1];
					f[i][7][1] = std::min(f[i+1][4][1], f[i+1][7][1]) + 7 * pow_ten[len - i - 1];
					if (!i)
						ans = std::min(ans, std::min(f[i][4][1], f[i][7][1]));
				} else {
					long long now = note[i] - '0';
					int pos = 0;
					for (; pos < 6; pos++)
						if (opt[pos] == note[i] - '0') break;
					if (now == 7) pos = 0;
					long long min0 = inf;
					for (int j = (pos & 1); j < 6; j += 2)
						min0 = std::min(min0, f[i+1][opt[j]][0]);

					if (min0 < inf && now != 7 && !(!i && (now == 4 || now == 1 || now == 2 || now == 5)))
						f[i][now][0] = min0 + now * pow_ten[len - i - 1];

					if (now == 7 || now == 4) {
						if (!i && now == 4) min0 = inf;
						min0 = std::min(min0, std::min(f[i+1][4][1], f[i+1][7][1]));
						f[i][now][1] = min0 + now * pow_ten[len - i - 1];
					}

					if (now == 8 || now == 5) {
						min0 = inf;
						for (int j = 1; j < 6; j += 2)
							min0 = std::min(min0, f[i+1][opt[j]][0]);
						f[i][now][1] = min0 + now * pow_ten[len - i - 1];
					}
					if (!i)
						ans = std::min(ans, std::min(f[i][now][0], f[i][now][1]));
				}
			}
			if (ans >= inf) return -1;
			else		    return ans;
		}
};


// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
bool KawigiEdit_RunTest(int testNum, string p0, bool hasAnswer, long long p1) {
	cout << "Test " << testNum << ": [" << "\"" << p0 << "\"";
	cout << "]" << endl;
	LuckySum *obj;
	long long answer;
	obj = new LuckySum();
	clock_t startTime = clock();
	answer = obj->construct(p0);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "\t" << p1 << endl;
	}
	cout << "Your answer:" << endl;
	cout << "\t" << answer << endl;
	if (hasAnswer) {
		res = answer == p1;
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
int main() {
	bool all_right;
	all_right = true;
	
	string p0;
	long long p1;
	
	{
	// ----- test 0 -----
	p0 = "?";
	p1 = 8ll;
	all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
	// ------------------
	}
	
	{
	// ----- test 1 -----
	p0 = "?1";
	p1 = 11ll;
	all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
	// ------------------
	}
	
	{
	// ----- test 2 -----
	p0 = "4?8";
	p1 = 448ll;
	all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
	// ------------------
	}
	
	{
	// ----- test 3 -----
	p0 = "2?" "?";
	p1 = -1ll;
	all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
	// ------------------
	}
	
	{
	// ----- test 4 -----
	p0 = "?" "?" "?" "?" "?" "?" "?" "?" "?" "?" "?" "?" "?" "?";
	p1 = 11888888888888ll;
	all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
	// ------------------
	}
	
	if (all_right) {
		cout << "You're a stud (at least on the example cases)!" << endl;
	} else {
		cout << "Some of the test cases had errors." << endl;
	}
	return 0;
}
// END KAWIGIEDIT TESTING
